package com.example.courselab.ui.feature.login

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import com.example.domain.network.ResultWrapper
import com.example.domain.usecase.LoginUseCase

class LoginViewModel(private val loginUseCase: LoginUseCase) : ViewModel() {
    private val _uiState = MutableStateFlow<LoginScreenEvent>(LoginScreenEvent.Initial)
    val uiState = _uiState.asStateFlow()

    fun login(username: String, password: String) {
        viewModelScope.launch {
            _uiState.value = LoginScreenEvent.Loading
            
            when (val result = loginUseCase.execute(username, password)) {
                is ResultWrapper.Success -> {
                    _uiState.value = LoginScreenEvent.Success
                }
                is ResultWrapper.Failure -> {
                    _uiState.value = LoginScreenEvent.Error("Login failed: ${result.error}")
                }
            }
        }
    }
}

sealed class LoginScreenEvent {
    data object Initial : LoginScreenEvent()
    data object Loading : LoginScreenEvent()
    data object Success : LoginScreenEvent()
    data class Error(val message: String) : LoginScreenEvent()
} 